/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fase2.pt.iul.sid.view;

import fase2.pt.iul.sid.model.ComentarioAoEstabelecimentoModel;
import fase2.pt.iul.sid.model.ComentarioAoPratoModel;
import fase2.pt.iul.sid.model.EstabelecimentoModel;
import fase2.pt.iul.sid.model.PratoModel;
import fase2.pt.iul.sid.model.UtilizadorModel;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ricardo Marques
 */
public class Comentarios extends javax.swing.JFrame {

    private DefaultTableModel model;
    private Vector<Vector<Object>> auxVectorSelection = null;
    /**
     * Creates new form Comentarios
     */
    public Comentarios() {
        initComponents();
        
        auxVectorSelection = new Vector<Vector<Object>>();
        
        DefaultTableModel model = new DefaultTableModel(null, getColumnsNames());
        jTable1.setModel(model);
        //jTable1.setEnabled(false);
        jTable1.setRowSelectionAllowed(true);
        jTable1.setColumnSelectionAllowed(false);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable1.setRowHeight(34);
        
        
        this.setResizable(false);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Página nº:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Proxima página");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setLabel("Página anterior");

        jButton3.setText("Ver comentario");

        jButton4.setText("Sair");

        jLabel4.setText("Nº comentários por página:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                        .addContainerGap(13, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.getAccessibleContext().setAccessibleName("labelNome");
        jLabel3.getAccessibleContext().setAccessibleName("labelNpagina");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed
   
    public void verListaComentariosPrato(PratoModel prato, List<ComentarioAoPratoModel> comentarios_prato){
        
       jLabel1.setText(prato.getDescricao());
        auxVectorSelection.clear();
        
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        
        for (ComentarioAoPratoModel esta : comentarios_prato) {
            
            Vector<Object> vObj = new Vector<Object>();
            vObj.add(esta.getComentario());
            vObj.add(esta.getUserName());
            vObj.add(esta.getUserEmail());
            data.add(vObj);
                
            //tabela auxiliar
            vObj = new Vector<Object>();
            vObj.add(esta.getIdEstabelecimento());
            vObj.add(esta.getIdPrato());
            auxVectorSelection.add(vObj);
            
        }  
     
        //Permite que a tabela não seja editavel
        model = new DefaultTableModel(data, getColumnsNames()) {
            @Override
            public boolean isCellEditable(int row, int column) {
               return false;
            }
        };
        jTable1.setModel(model);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(200); 
        Dimension d = jTable1.getPreferredSize();
        jScrollPane1.setPreferredSize(new Dimension(d.width,22+(34*comentarios_prato.size()+1)));
        
        Dimension d1 = this.getPreferredSize();
        this.setSize(new Dimension(d1.width,236+(34*comentarios_prato.size()+1)));
        
    }
    
    public void verListaComentariosEstabelecimento(EstabelecimentoModel estabelecimento, List<ComentarioAoEstabelecimentoModel> comentarios_estabelecimento){
        
        jLabel1.setText(estabelecimento.getDesignacao());
        auxVectorSelection.clear();
        
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        
        for (ComentarioAoEstabelecimentoModel esta : comentarios_estabelecimento) {
            
            Vector<Object> vObj = new Vector<Object>();
            vObj.add(esta.getComentario());
            vObj.add(esta.getUserName());
            vObj.add(esta.getUserEmail());
            data.add(vObj);
                
            //tabela auxiliar
            vObj = new Vector<Object>();
            vObj.add(esta.getIdEstabelicimento());
            vObj.add(0);
            auxVectorSelection.add(vObj);
            
        }  
     
        //Permite que a tabela não seja editavel
        model = new DefaultTableModel(data, getColumnsNames()) {
            @Override
            public boolean isCellEditable(int row, int column) {
               return false;
            }
        };
        jTable1.setModel(model);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(200); 
        Dimension d = jTable1.getPreferredSize();
        jScrollPane1.setPreferredSize(new Dimension(d.width,22+(34*comentarios_estabelecimento.size()+1)));
        
        Dimension d1 = this.getPreferredSize();
        this.setSize(new Dimension(d1.width,236+(34*comentarios_estabelecimento.size()+1)));
        
    }
    
    private Vector<String> getColumnsNames(){
        
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("Comentário");
        columnNames.add("Autor");
        columnNames.add("Email");
        return columnNames;
        
    }
    
    public void adcionarListner(ActionListener listner){
        
        this.jComboBox1.addActionListener(listner);
        
    }
    
    public void adcionarBtnsListner(ActionListener listner){
        
        this.jComboBox1.addActionListener(listner);
        this.jButton1.addActionListener(listner);
        this.jButton2.addActionListener(listner);
        this.jButton3.addActionListener(listner);
        this.jButton4.addActionListener(listner);
        
    }

    public JButton getjButton1() {
        return jButton1;
    }

    public JButton getjButton2() {
        return jButton2;
    }

    public JButton getjButton3() {
        return jButton3;
    }

    public JButton getjButton4() {
        return jButton4;
    }

    public JComboBox getjComboBox1() {
        return jComboBox1;
    }

    public JLabel getjLabel3() {
        return jLabel3;
    }
    
    public Vector<Vector<Object>> getAuxVectorSelection() {
        return auxVectorSelection;
    }  

    public JTable getjTable1() {
        return jTable1;
    }

    public DefaultTableModel getModel() {
        return model;
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
